﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HotelSoft.HotelManagement_BUS;
using HotelSoft.HotelManagement_DTO;
using System.Data.Sql;
using System.Data.SqlClient;
namespace HotelSoft.HotelManagement_GUI
{
    public partial class frmConnection : Form
    {
        public frmConnection()
        {
            InitializeComponent();
        }
        Double temp = 1;
        private void frmConnection_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        private void fillCmbServerList()
        {
                try
                {
                    string myServer = Environment.MachineName;

                    DataTable servers = new DataTable();
                    servers = SqlDataSourceEnumerator.Instance.GetDataSources();
                    foreach (DataRow dr in servers.Rows)
                    {
                        if (myServer == dr["ServerName"].ToString()) ///// used to get the servers in the local machine////
                        {
                            if ((dr["InstanceName"] as string) != null)
                                cmbServList.Items.Add(dr["ServerName"].ToString() + "\\" + dr["InstanceName"].ToString());
                            else
                                cmbServList.Items.Add(dr["ServerName"].ToString());
                        }
                    }
                    if (cmbServList.Items.Count > 0)
                        cmbServList.SelectedIndex = 0;
                    else
                        cmbServList.Text = "<No available SQL Servers>";
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
        }

        private void ckTrusted_CheckedChanged(object sender, EventArgs e)
        {
            if (ckTrusted.Checked)
                grInfo.Enabled = false;
            else
                grInfo.Enabled = true;
        }

        private void loadComboDBList()
        {
            try
            {
                SqlDataDTO dtoSQL = new SqlDataDTO();
                string SQL = "";
                dtoSQL.SERVERNAME = cmbServList.SelectedItem.ToString();
                if (ckTrusted.Checked)
                {
                    dtoSQL.USERNAME = null;
                    dtoSQL.PASSWORD = null;
                    dtoSQL.DATABASE = null;
                    dtoSQL.TRUSTED = true;
                    SQL = "Data Source=" + dtoSQL.SERVERNAME + "; Integrated Security=SSPI;";
                }
                else
                {
                    dtoSQL.USERNAME = txtUser.Text;
                    dtoSQL.PASSWORD = txtPwd.Text;
                    dtoSQL.DATABASE = null;
                    dtoSQL.TRUSTED = false;
                    SQL = "Data Source=" + dtoSQL.SERVERNAME + ";User Id=" + dtoSQL.USERNAME + ";Password=" + dtoSQL.PASSWORD + ";";
                }
                SqlConnection sqlConx = new SqlConnection(SQL);
                sqlConx.Open();
                DataTable tblDatabases = sqlConx.GetSchema("Databases");
                sqlConx.Close();

                cmbDataName.DisplayMember = "database_name";
                cmbDataName.ValueMember = "database_name";
                cmbDataName.DataSource = tblDatabases;
                if (this.cmbDataName.Items.Count != 0)
                    cmbDataName.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnCheckConnection_Click(object sender, EventArgs e)
        {
            SqlDataDTO dtoSQL = new SqlDataDTO();
            if (cmbServList.Text != "") dtoSQL.SERVERNAME = cmbServList.Text;
            else
            {
                MessageBox.Show("Xin hãy chọn Server", "Thông báo kết nối CSDL");
                return;
            }
            dtoSQL.TRUSTED=ckTrusted.Checked?true:false;
            if (dtoSQL.TRUSTED == true)
            {
                dtoSQL.USERNAME = null;
                dtoSQL.PASSWORD = null;
            }
            else
            {
                dtoSQL.USERNAME = txtUser.Text;
                dtoSQL.PASSWORD = txtPwd.Text;
            }
            if (cmbDataName.Text != "") dtoSQL.DATABASE = cmbDataName.Text;
            else
            {
                MessageBox.Show("Xin hãy chọn CSDL (Database name)", "Thông báo kết nối CSDL");
                return;
            }

            SqlDataBUS busSQL = new SqlDataBUS();
            busSQL.SqlData(dtoSQL);
            busSQL.ChangeConnectionString();
            if (busSQL.doLogin())
            {
                MessageBox.Show("Kết nối thành công!", "Thông báo kết nối CSDL SQL");
                this.Close();
            }
            else
                MessageBox.Show("Kết nối không thành công!", "Thông báo kết nối CSDL SQL");
            
        }

        private void cmbDataName_Enter(object sender, EventArgs e)
        {
            if (cmbServList.Text == "")
            {
                MessageBox.Show("Xin vui chọn Server name!", "Thông báo kết nối CSDL");
                return;
            }
            if (!ckTrusted.Checked)
            {
                MessageBox.Show("        Xin vui lòng nhập Username & Password! \n(nếu không phải kết nối tin cậy 'Trusted Connection')","Thông báo kết nối CSDL");
                txtUser.Focus();
                return;

            }
            loadComboDBList();
        }

        private void cmbServList_Enter(object sender, EventArgs e)
        {
            fillCmbServerList();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Opacity = this.Opacity + 0.1;
            if (this.Opacity == temp)
                timer1.Enabled = false;
        }
    }
}
